﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Tsanie.DmPoster.Component {
    public class KeyTextBox : TextBox {
        [Description("咱就不让你修改，有脾气？")]
        public new string Text {
            get {
                return base.Text;
            }
        }
        private Keys _key = Keys.None;
        [Description("获取或设置表示的快捷键")]
        public Keys Key {
            get { return _key; }
            set {
                _key = value;
                RefreshText(value);
            }
        }
        public KeyTextBox() { this.ReadOnly = true; }
        private bool RefreshText(Keys key) {
            Keys mod = key & Keys.Modifiers;
            Keys chars = key & Keys.KeyCode;
            string modStr = "";
            if ((mod & Keys.Control) == Keys.Control)
                modStr += "Cr+";
            if ((mod & Keys.Shift) == Keys.Shift)
                modStr += "Sf+";
            if ((mod & Keys.Alt) == Keys.Alt)
                modStr += "Al+";
            if (chars >= Keys.Space && chars <= Keys.F24) {
                modStr += chars;
            }
            base.Text = modStr;
            return true;
        }
        private void PreKey(Keys key) {
            Keys chars = key & Keys.KeyCode;
            if (chars >= Keys.Space && chars <= Keys.F24) {
                this.Key = key;
            } else {
                RefreshText(key);
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            PreKey(keyData);
            return true;
        }
        protected override void OnKeyUp(KeyEventArgs e) {
            base.OnKeyUp(e);
            RefreshText(this.Key);
        }
    }
}
